package com.example.controller;


import com.alibaba.dubbo.config.annotation.Reference;

import com.example.domain.message.MessageConstant;
import com.example.domain.pojo.OrderSetting;
import com.example.domain.result.Result;
import com.example.domain.utils.POIUtils;
import com.example.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import javax.xml.crypto.Data;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.logging.SimpleFormatter;

//预约管理
@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    //注入Service
   @Autowired
    private OrderSettingService orderSettingService;

    //文件上传:name文件名称 根据文件名称上传指定文件 参数类型
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile){
        //1:根据上传的文件,利用POI解析上传文件的数据,封装的有工具类
        try {
            //2:将文档中的数据解析后存入list集合中
            List<String[]> strings = POIUtils.readExcel(excelFile);
            //创建一个空的集合,用于存放预约实体类
            List<OrderSetting> list=new ArrayList<>();
            //3:判断用户提交的表格是否有值
            if(strings!=null){
                //4:遍历list集合,取出每一条数据,封装在实体类中
                for (String[] string : strings) {
                    //取出数组中的内容
                    for (int i=0;i<string.length;i++) {
                        //将数组中的字符串格式化日期类型
                        Date date=new Date(string[0]);
                        //将人数转换为int类型
                        int number=Integer.parseInt(string[1]);
                        OrderSetting order = new OrderSetting(date,number);
                        //将转换的实体类添加到集合
                        list.add(order);
                    }
                }
            }
            //5:调用service方法,传递list集合
            orderSettingService.add(list);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (IOException e) {
//            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }

    }

    //查询预约表,查询某一年某一月的预约信息
    @RequestMapping("/findAllBymonth")
    public Result findAll(String date){
        try {
            System.out.println(date);
            List<Map> allBymonth = orderSettingService.findAllBymonth(date);
            return  new Result(true,MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS,allBymonth);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL);
        }
    }
    //编辑可预约人数,deitNUmberByDate
    @RequestMapping("/editNumberByDate")
    public Result editNumber(@RequestBody OrderSetting orderSetting){
        try {
            orderSettingService.deitNumberByDate(orderSetting);
            return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
//            e.printStackTrace();
            return new Result(false,MessageConstant.ORDERSETTING_FAIL);
        }
    }

}
